<!--数据字典下拉组件  *输入项的查询参数在前端筛选字典值，暂时支持value值的筛选-->
<!--@param dictType 指定的数据字典-->
<!--@param filterKey 指定的字典值value -->
<!--@param flag  根据指定值进行筛选展示，默认为正向筛选，false时为反向筛选，value为空时不生效-->
<template>
  <el-select v-model="dValue"
             filterable :clearable="clearable" remote="false"
             :style="{width: '100%'}">
    <el-option
      v-for="item in dataList"
      :key=item.dictValue
      :label=item.dictLabel
      :value="item.dictValue"
    />
  </el-select>

</template>
<script>
import request from '@/utils/request'
import useDictStore from '@/store/modules/dict'
export default {
  model: {
    prop: 'selectValue',
    event: 'irCurveChange',
    dictType:'',
    filterKey:'',
    flag:true,
    clearable:true,
  },
  props: {
    dataList: Array,
    selectValue:String,
    dictType:String,
    filterKey:{
      type:String,
      default:''
    },
    flag:String,
    clearable:{
      type:Boolean,
      default: true
    }
  },
  watch: {
    filterKey:{
      handler(v){
        this.getDataList();
      }
    }
  },
  data() {
    return {
      dataList: [],
      dValue:this.selectValue,
      dictType:this.dictType
    }
  },
  created() {
    this.getDataList();
  },
  methods: {
    /** 查询下拉列表 */
    getDataList() {
      let dicts = useDictStore().getDict(this.dictType);
      if (dicts) {
        this.filterDictVal(dicts);
      } else {
        request({
          url: '/system/dict/data/type/' + this.dictType,
          method: 'get'
        }).then(response => {
          let tmpData = response.data;
          //将所有字典放入缓存
          useDictStore().setDict(this.dictType,response.data);
          this.filterDictVal(tmpData);
        });
      }
    },
    filterDictVal(dictObj){
      if (dictObj != null && dictObj.length > 0) {
        this.dataList = dictObj.filter((value, key, arr) => {
          if (this.filterKey != null && this.filterKey != '') {
            if (this.flag === 'false') {
              //反向筛选
              return String(this.filterKey).split(',').findIndex(v => v == value.dictValue) == -1;
            } else {
              //正向筛选
              return String(this.filterKey).split(',').findIndex(v => v == value.dictValue) != -1;
            }
          } else {
            return value;
          }
        })
      }
    }
  },

}
</script>
